package com.breaking.second.activity.pager.my;

import android.content.Intent;
import android.os.Bundle;
import android.view.MenuItem;
import android.view.View;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.widget.Toolbar;
import androidx.recyclerview.widget.StaggeredGridLayoutManager;

import com.alibaba.fastjson2.JSONObject;
import com.breaking.second.R;
import com.breaking.second.activity.BuyGoodsActivity;
import com.breaking.second.activity.EditUserActivity;
import com.breaking.second.activity.MyGoodsActivity;
import com.breaking.second.activity.SellGoodsActivity;
import com.breaking.second.activity.TransactionActivity;
import com.breaking.second.activity.login.LoginActivity;
import com.breaking.second.activity.money.MoneyActivity;
import com.breaking.second.activity.pager.BaseFragment;
import com.breaking.second.activity.pager.adapter.MyToolAdapter;
import com.breaking.second.application.EscortApplication;
import com.breaking.second.databinding.PagerMyBinding;
import com.breaking.second.entity.AllMoneyEntity;
import com.breaking.second.entity.BookEntity;
import com.breaking.second.entity.MyToolEntity;
import com.breaking.second.entity.UserLoginEntity;
import com.breaking.second.recycler.RecyclerItemClickListener;
import com.breaking.second.utils.SecurePreferencesUtil;
import com.breaking.second.utils.compat.RecyclerCompat;
import com.breaking.second.utils.okhttp.OkhttpUtil;

import java.util.ArrayList;
import java.util.List;

import okhttp3.FormBody;

//我的界面
public class MyPager extends BaseFragment<PagerMyBinding> {
    private final List<MyToolEntity> myToolEntities = new ArrayList<>();

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        initData();
        initView();
        initRecycler();
    }

    private void initRecycler() {
        myToolEntities.add(new MyToolEntity(R.mipmap.ic_buy_128, "我的商品"));
        myToolEntities.add(new MyToolEntity(R.mipmap.ic_buy_128, "已购买商品"));
        myToolEntities.add(new MyToolEntity(R.mipmap.ic_sell_128, "已卖出商品"));
        myToolEntities.add(new MyToolEntity(R.mipmap.ic_sell_128, "交易记录"));
        MyToolAdapter myToolAdapter = new MyToolAdapter(requireContext(), myToolEntities);
        binding.recycler.setLayoutManager(new StaggeredGridLayoutManager(4, StaggeredGridLayoutManager.VERTICAL));
        binding.recycler.setAdapter(myToolAdapter);
        binding.recycler.addOnItemTouchListener(new RecyclerItemClickListener(requireContext(), this::onItemClick));
        RecyclerCompat.SpacesItemDecoration(binding.recycler);
    }


    public void onItemClick(View view, int position) {
        MyToolEntity myToolEntity = myToolEntities.get(position);
        if (myToolEntity.getTitle().equals("我的商品")) {
            startActivity(new Intent(requireContext(), MyGoodsActivity.class));
        } else if (myToolEntity.getTitle().equals("已购买商品")) {
            startActivity(new Intent(requireContext(), BuyGoodsActivity.class));
        } else if (myToolEntity.getTitle().equals("已卖出商品")) {
            startActivity(new Intent(requireContext(), SellGoodsActivity.class));
        } else if (myToolEntity.getTitle().equals("交易记录")) {
            startActivity(new Intent(requireContext(), TransactionActivity.class));
        }
    }

    private void initView() {
        binding.moneyA.setOnClickListener(this::MoneyA);
        binding.toolbar.setOnMenuItemClickListener(this::OnMenuItemClick);
        binding.back.setOnClickListener(this::backLogin);
    }

    private void backLogin(View v) {
        SecurePreferencesUtil securePreferencesUtil = new SecurePreferencesUtil(UserLoginEntity.class.getName());
        securePreferencesUtil.removeKey(requireContext(), "data");
        requireActivity().finish();
        startActivity(new Intent(requireContext(), LoginActivity.class));
    }

    private boolean OnMenuItemClick(MenuItem menuItem) {
        startActivity(new Intent(requireContext(), EditUserActivity.class));
        return true;
    }

    private void MoneyA(View view) {
        startActivity(new Intent(requireContext(), MoneyActivity.class));
    }

    private void initData() {
        initUserData();
    }

    private void initUserData() {
        UserLoginEntity userLoginEntity = EscortApplication.getUserLoginEntity();
        if (userLoginEntity == null) return;
        binding.userId.setText(String.valueOf(userLoginEntity.getId()));
        binding.nick.setText(userLoginEntity.getUsername());
        binding.money.setText(String.valueOf(userLoginEntity.getMoney()));
    }

    @Override
    public void onResume() {
        super.onResume();
        getAllMoney();
    }

    public void getAllMoney() {
        FormBody.Builder form = new FormBody.Builder();
        form.add("userId", String.valueOf(pageableVo.getUserId()));
        OkhttpUtil.Get("trading/allMoney", form, new OkhttpUtil.CallbackListener(this::getAllMoney));
    }

    private void getAllMoney(BookEntity bookEntity) {
        if (bookEntity.isCode()) {
            AllMoneyEntity entity = bookEntity.to(AllMoneyEntity.class);
            if (entity == null) return;
            binding.TotalRevenue.setText(String.valueOf(entity.getTotalRevenue()));
            binding.TotalSpending.setText(String.valueOf(entity.getTotalSpending()));

        }
    }
}
